package bufferings.lab.basic.speed;

import org.junit.Test;
import org.slim3.tester.AppEngineTestCase;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Transaction;

public class Crud1EntitySpeed extends AppEngineTestCase {

  public static final String KIND = Crud1EntitySpeed.class.getSimpleName();

  public static final String KEY_NAME = "keyName";

  @Test
  public void putTx外_getTx外() throws Exception {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    ds.get((Transaction) null, KeyFactory.createKey(KIND, KEY_NAME));
  }

  @Test
  public void putTx外_getTx内() throws Exception {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    Transaction tx = ds.beginTransaction();
    ds.get(tx, KeyFactory.createKey(KIND, KEY_NAME));
    tx.commit();
  }

  @Test
  public void putTx外() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
  }

  @Test
  public void putTx内() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Transaction tx = ds.beginTransaction();
    ds.put(tx, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    tx.commit();
  }

  @Test
  public void putTx外_putTx外() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
  }

  @Test
  public void putTx外_putTx内() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    Transaction tx = ds.beginTransaction();
    ds.put(tx, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    tx.commit();
  }

  @Test
  public void putTx外_deleteTx外() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    ds.delete((Transaction) null, KeyFactory.createKey(KIND, KEY_NAME));
  }

  @Test
  public void putTx外_deleteTx内() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    ds
      .put((Transaction) null, new Entity(KeyFactory.createKey(KIND, KEY_NAME)));
    Transaction tx = ds.beginTransaction();
    ds.delete(tx, KeyFactory.createKey(KIND, KEY_NAME));
    tx.commit();
  }
}
